#region Using

using System;
using System.Text;
using System.Collections.Specialized;

#endregion

namespace SemanticEngine
{
	public abstract class ValidationBase
	{

		private StringDictionary _BrokenRules = new StringDictionary();

		/// <summary>
		/// Add or remove a broken rule.
		/// </summary>
		/// <param name="propertyName">The name of the property.</param>
		/// <param name="errorMessage">The description of the error</param>
		/// <param name="isBroken">True if the validation rule is broken.</param>
		protected void AddRule(string propertyName, string errorMessage, bool isBroken)
		{
			if (isBroken)
			{
				_BrokenRules[propertyName] = errorMessage;
			}
			else
			{
				if (_BrokenRules.ContainsKey(propertyName))
				{
					_BrokenRules.Remove(propertyName);
				}
			}
		}

		/// <summary>
		/// Reinforces the business rules by adding rules to the
		/// broken rules collection.
		/// </summary>
		protected abstract void Validate();

		/// <summary>
		/// Gets whether the object is valid or not.
		/// </summary>
		public bool IsValid
		{
			get
			{
				Validate();
				return this._BrokenRules.Count == 0;
			}
		}

		/// /// <summary>
		/// If the object has broken business rules, use this property to get access
		/// to the different validation messages.
		/// </summary>
		public string ValidationMessage
		{
			get
			{
				StringBuilder sb = new StringBuilder();
				foreach (string messages in this._BrokenRules.Values)
				{
					sb.AppendLine(messages);
				}

				return sb.ToString();
			}
		}

	}
}